<template>
  <div class="cool-background">
    <div class="login_box">
      <div class="title">测试管理平台</div>
      <el-form
        :model="user"
        ref="rulesRef"
        class="login_from"
        :rules="rulesLogin"
      >
        <!-- 登录表单 -->
        <el-form-item prop="username">
          <el-input
            v-model="user.username"
            prefix-icon="el-icon-user-solid"
            placeholder="请输入账户"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="user.password"
            prefix-icon="el-icon-s-goods"
            placeholder="请输入密码"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item label="记住账户" size="mini">
          <el-switch v-model="status"></el-switch>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" style="width: 100%" @click="login"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import * as account from "../api/account.js";
import * as cookies from "../util/cookies";
export default {
  data() {
    return {
      //登录对象
      user: {
        username: "",
        password: "",
      },
      //记住账户状态
      status: false,
      rulesLogin: {
        // 验证用户名是否合法
        username: [
          { required: true, message: "请输入登录账户", trigger: "blur" },
        ],
        // 验证密码是否合法
        password: [
          { required: true, message: "请输入登录密码", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {
    //给window对象绑定keydown事件
    window.addEventListener("keydown", this.keyDown);
    //获取记住用户名
    const username = window.localStorage.getItem("username");
    if (username) {
      this.user.username = username;
      this.status = true;
    }
  },
  //vue 生命周期销毁钩子
  destroyed() {
    //给window对象移除keydown事件
    window.removeEventListener("keydown", this.keyDown, false);
  },
  methods: {
    login() {
      account.login(this.user).then((response) => {
        let result = response.data;
        //登录失败
        if (result.code !== 0) {
          this.$message({
            message: result.message,
            type: "error",
          });
          return;
        }

        //登录成功
        let user = result.data;
        //保存登录信息cookies
        cookies.setCurrentUser(user);
        //设置当前用户
        this.$store.commit("setCurrentUser", user);
        //如果当前有默认选择项目，则跳转到[项目首页]；否则，跳转到[所有项目]页
        let redirect = "/project";
        let currentProject = this.$store.getters.getCurrentProject;
        if (!currentProject || currentProject.username != user.username) {
          this.$message({
            message: "你还未选择项目，请先选择项目",
            type: "info",
          });
          redirect = "/allProject";
        }
        //基础项目，先跳转到用户管理
        // let redirect = '/user'
        this.$router.push(redirect);

        if (this.status) {
          window.localStorage.setItem("username", this.user.username);
        } else {
          window.localStorage.removeItem("username");
        }
      });
    },
    keyDown(e) {
      //如果按下回车直接登录
      if (e.keyCode == 13) {
        this.login();
      }
    },
    animateBackground() {
      const background = this.$el.querySelector(".cool-background");
      background.style.animation = "coolAnimation 10s infinite";
    },
  },
};
</script>


<style scoped>
.title {
  position: absolute;
  left: 50%;
  top: 25%;
  font-size: 24px;
  line-height: 50px;
  text-align: center;
  transform: translate(-50%, -50%);
  font-weight: bold;
  color: #409eff;
}

.cool-background {
  /* background: rgb(255, 255, 255); */
  height: 100%;
  height: 100vh;
  background: linear-gradient(to right, #f1f2f5, #7f8ca8, #717dbc);
}

@keyframes coolAnimation {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
.login_box {
  width: 620px;
  height: 400px;
  box-shadow: 0 0 10px rgb(21, 21, 90);
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.login_from {
  position: absolute;
  width: 100%;
  bottom: 0;
  padding: 5% 10%;
  box-sizing: border-box;
}

.btns {
  width: 100%;
  position: flex;
  float: right;
  justify-content: flex-end;
}
</style>
